package com.pig4cloud.pig.module.service.charger.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.common.mybatis.helper.RequestDataHelper;
import com.pig4cloud.pig.module.api.entity.Parameter;
import com.pig4cloud.pig.module.api.entity.charger.ChargerOrder;
import com.pig4cloud.pig.module.api.entity.charger.ChargerPortRecord;
import com.pig4cloud.pig.module.mapper.charger.ChargerOrderMapper;
import com.pig4cloud.pig.module.mapper.charger.ChargerPortRecordMapper;
import com.pig4cloud.pig.module.service.base.IParameterService;
import com.pig4cloud.pig.module.service.charger.IChargerPortRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  设备充电记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-07
 */
@Service
public class ChargerPortRecordServiceImpl extends ServiceImpl<ChargerPortRecordMapper, ChargerPortRecord> implements IChargerPortRecordService {

    @Resource
    private ChargerPortRecordMapper portRecordMapper;
    @Resource
    private ChargerOrderMapper chargerOrderMapper;
    @Resource
    private IParameterService parameterService;


    @Override
    public int addNowMonthRecordTable() {
        try {
            Parameter parameter = parameterService.getParameterById(1);
            if(parameter != null){
                String tableName = "";
                if(StrUtil.isNotEmpty(parameter.getChargerPortRecordName())){
                    tableName = parameter.getChargerPortRecordName();
                    System.out.println("缓存记录表名：" + tableName);
                }

                String tableNameNow = "charger_port_record_" + DateUtils.getMonthStr(new Date());
                System.out.println("当前月份的表名：" + tableNameNow);

                if(!tableName.equals(tableNameNow)){
                    int isExits = chargerOrderMapper.isTableExist(tableNameNow);
                    if(isExits == 0){
                        portRecordMapper.createRecordTable(tableNameNow);

                        parameter.setChargerPortRecordName(tableNameNow);
                        parameterService.updateById(parameter);
                        parameterService.resetParameter(1);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    @Override
    public List<ChargerPortRecord> getRecordListByOrderNo(ChargerOrder chargerOrder) {
        Map<String,Object> map = new HashMap<>();
        map.put("charger_port_record", chargerOrder.getRecordTableName());
        RequestDataHelper.setRequestData(map);

        QueryWrapper<ChargerPortRecord> qw = new QueryWrapper<>();
        qw.eq("order_no",chargerOrder.getOrderNo());
        qw.orderByAsc("create_time");
        List<ChargerPortRecord> list = portRecordMapper.selectList(qw);
        return list;
    }

    @Override
    public Page<ChargerPortRecord> getRecordListByOrderNo(Page<ChargerPortRecord> page, ChargerOrder chargerOrder) {

        Map<String,Object> map = new HashMap<>();
        map.put("charger_port_record", chargerOrder.getRecordTableName());
        RequestDataHelper.setRequestData(map);

        QueryWrapper<ChargerPortRecord> qw = new QueryWrapper<>();
        qw.eq("order_no",chargerOrder.getOrderNo());
        qw.orderByAsc("create_time");
        Page<ChargerPortRecord> list = portRecordMapper.selectPage(page, qw);
        return list;
    }

}
